package com.hezhi.project.module.leave.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hezhi.project.module.leave.domain.Leave;
import com.hezhi.project.module.leave.service.ILeaveService;
import com.hezhi.framework.web.controller.BaseController;
import com.hezhi.framework.web.page.TableDataInfo;
import com.hezhi.framework.web.domain.AjaxResult;

/**
 * 请假 信息操作处理
 * 
 * @author hezhi
 * @date 2018-08-19
 */
@Controller
@RequestMapping("/module/leave")
public class LeaveController extends BaseController
{
    private String prefix = "module/leave";
	
	@Autowired
	private ILeaveService leaveService;
	
	@RequiresPermissions("module:leave:view")
	@GetMapping()
	public String leave()
	{
	    return prefix + "/leave";
	}
	
	/**
	 * 查询请假列表
	 */
	@RequiresPermissions("module:leave:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Leave leave)
	{
		startPage();
        List<Leave> list = leaveService.selectLeaveList(leave);
		return getDataTable(list);
	}
	
	/**
	 * 新增请假
	 */
	@RequiresPermissions("module:leave:add")
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改请假
	 */
	@RequiresPermissions("module:leave:edit")
	@GetMapping("/edit/{leaveId}")
	public String edit(@PathVariable("leaveId") Integer leaveId, Model model)
	{
		Leave leave = leaveService.selectLeaveById(leaveId);
		model.addAttribute("leave", leave);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存请假
	 */
	@RequiresPermissions("module:leave:save")
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Leave leave)
	{
		if (leaveService.saveLeave(leave) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除请假
	 */
	@RequiresPermissions("module:leave:remove")
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = leaveService.deleteLeaveByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	/**
	 * 修改状态
	 * @param leave
	 * @return
	 */
	@PostMapping("/updateStatus")
	@ResponseBody
	public AjaxResult updateStatus(Leave leave){
		int rows = leaveService.updateLeave(leave);
		if(rows > 0){
			return success();
		}
		return error();
	}
	
}
